package de.start;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;

import de.morris.Start;

public class MainGui extends JFrame implements ActionListener {

	private JPanel loginPane;

	private de.morris.Start start;

	private JButton startButton;
	private JTextField userInputText;
	private JTextField userInputText2;

	public MainGui() {
		super();
		loginPane = new JPanel();
		this.setSize(300, 200);
		this.setContentPane(loginPane);
		GroupLayout layout = new GroupLayout(loginPane);
		loginPane.setLayout(layout);

		JLabel userLabel = new JLabel("Spieler 1");
		JLabel userLabe2 = new JLabel("Spieler 2");
		userInputText = new JTextField("Player 1");
		userInputText.setMaximumSize(new Dimension(200, 50));
		userInputText2 = new JTextField("Player 2");
		userInputText2.setMaximumSize(new Dimension(200, 50));
		startButton = new JButton("start");
		

		startButton.addActionListener(this);
		// Turn on automatically adding gaps between components
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		// Create a sequential group for the horizontal axis.
		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();

		// The sequential group in turn contains two parallel groups.
		// One parallel group contains the labels, the other the text fields.
		// Putting the labels in a parallel group along the horizontal axis
		// positions them at the same x location.
		//
		// Variable indentation is used to reinforce the level of grouping.
		hGroup.addGroup(layout.createParallelGroup().addComponent(userLabel)
				.addComponent(userLabe2).addComponent(startButton));
		hGroup.addGroup(layout.createParallelGroup()
				.addComponent(userInputText).addComponent(userInputText2));
		layout.setHorizontalGroup(hGroup);

		// Create a sequential group for the vertical axis.
		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

		// The sequential group contains two parallel groups that align
		// the contents along the baseline. The first parallel group contains
		// the first label and text field, and the second parallel group
		// contains
		// the second label and text field. By using a sequential group
		// the labels and text fields are positioned vertically after one
		// another.
		vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
				.addComponent(userLabel).addComponent(userInputText));
		vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
				.addComponent(userLabe2).addComponent(userInputText2));
		vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
				.addComponent(startButton));
		layout.setVerticalGroup(vGroup);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		start = new Start(userInputText.getText(), userInputText2.getText());		
	}

	public void addExitListener(){
		this.addWindowListener( new WindowAdapter() {
	          public void windowClosing( WindowEvent e ) { System.exit(0); }
		});	
	}
	
	public static void main(String[] args) {
		MainGui mg = new MainGui();
		mg.setVisible(true);
		mg.addExitListener();
	}
}
